package com.mybatis.generator.codegen;

import com.mybatis.generator.config.CodeGeneratorConfig;
import com.mybatis.generator.utils.FileUtil;
import com.mybatis.generator.utils.GenUtil;
import freemarker.template.Configuration;
import freemarker.template.Template;

import java.io.IOException;
import java.util.Map;

public class CodeGeneratorService {

    private Configuration cfg;
    private CodeGeneratorConfig config;
    private String className;

    public CodeGeneratorService(Configuration cfg, CodeGeneratorConfig config, String className) {
        this.cfg = cfg;
        this.config = config;
        this.className = className;
    }

    public void build(Map<String, Object> variables, boolean isOverwrite) throws IOException {
        variables.put("packageService", config.getClassPackageService());
        variables.put("packageServiceImpl", config.getClassPackageServiceImpl());

        Template templateService = cfg.getTemplate(config.getTemplateFileNameService());
        FileUtil.buildTemplate(variables, config.getClassPathService(), config.getClassNameServicePrefix() + GenUtil.firstCharacterToUpper(className) + config.getClassNameServiceSuffix() + ".java", templateService, isOverwrite);

        Template templateServiceImpl = cfg.getTemplate(config.getTemplateFileNameServiceImpl());
        FileUtil.buildTemplate(variables, config.getClassPathServiceImpl(), config.getClassNameServiceImplPrefix() + GenUtil.firstCharacterToUpper(className) + config.getClassNameServiceImplSuffix() + ".java", templateServiceImpl, isOverwrite);
    }
}
